package com.hsav.kcxb.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.List;

/**
 * Created by zhao
 */

public class RecyclerBaseAdapter<T> extends RecyclerView.Adapter {

    private final Context mContext;
    private final List<T> mDataList;
    private final BaseAdapterManager mAdapterManager;

    public RecyclerBaseAdapter(Context context,BaseAdapterManager adapterManager){
        this.mContext = context;
        this.mAdapterManager= adapterManager;
        this.mDataList = adapterManager.getDataList();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
         View itemView = LayoutInflater.from(mContext).inflate(mAdapterManager.getLayoutId(), parent, false);
        MyViewHolder myViewHolder = new MyViewHolder(itemView);
        return myViewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        MyViewHolder myViewHolder = (MyViewHolder)holder;
        myViewHolder.setData(position);
    }

    @Override
    public int getItemCount() {
        if(mDataList!=null){
            return mDataList.size();
        }
        return 0;
    }

    private class MyViewHolder extends RecyclerView.ViewHolder {
        public MyViewHolder(View itemView) {
            super(itemView);
            mAdapterManager.initItemView(itemView);
        }

        public void setData(int position) {
            mAdapterManager.bindViewData(position);
        }
    }
}
